#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <wait.h>
#include <string.h>

void func()
{
    char msg[1024] = {0};
    while(1)
    {
        scanf("%s",msg);
        if(strcmp(msg,"quit") == 0)
            exit(100);
        printf("%s\n",msg);
    }
}

int main()
{
    pid_t pid = fork();
    if(pid < 0)
    {
        perror("fork");
        return -1;
    }
    if(pid == 0)
    {
        printf("这是一条子进程,进程号是:%d 父进程号是:%d\n",getpid(),getppid());
        sleep(1);
        func();
        printf("子进程结束\n");
        return 2;
    }
    else
    {
        printf("这是一条父进程,进程号是:%d,子进程号是:%d\n",getpid(),pid);
        int result = 0;
        waitpid(pid,&result,0);
        printf("子进程已经结束,返回值是%d\n",WEXITSTATUS(result));
    }

    return 0;
}